<template>
  <div class="insight common-style">
  	<div class="insight-header">
  		<div class="fl">
  			<input class="input" v-model="searchContent">
  			<button class="Public-btn" @click="searchList()">搜索</button>
  		</div>
  		<button class="Public-btn fr" @click="insightFn('add')">新增</button>
  	</div>
  	<div>
  		<span class="eg">洞察列表内部链接示例：comprehensivePolicy/comprehensivePolicy?type=14</span>
  		<span class="eg">洞察详情内部链接示例：articleDetail/articleDetail?judgeType=&id=xxx</span>
  	</div>
  	<div class="table-father">
	    <table class="table">
				<thead>
					<th>序号</th>
					<th>名称</th>
					<th>发布人</th>
					<th>发布时间</th>
					<th>浏览量</th>
					<th>id值</th>
					<th>状态</th>
					<th>操作</th>
				</thead>
				<tbody>
					<tr v-for="(item,index) in tableList">
						<td><span>{{index+1}}</span></td>
						<td class="cursor" :title="item.title">
							<span v-if="item.title.length>15">{{item.title.substring(0,15)}}....</span>
							<span v-if="item.title.length<16">{{item.title}}</span>
						</td>
						<td><span>{{item.publishBy}}</span></td>
						<td><span>{{item.publishAt}}</span></td>
						<td><span>{{item.pageView}}</span></td>
						<td><span>{{item.id}}</span></td>
						<td>
							<span v-if="item.status==1">待发布</span>
							<span v-if="item.status==2">已发布</span>
						</td>
						<td>
							<button class="Public-btn" @click="insightFn('lookOver',item.id)">查看</button>
							<button class="Public-btn" v-if="item.status==1" @click="insightFn('modify',item.id)">修改</button>
							<button class="Public-btn" v-if="item.status==1" @click="publishData(item.id,'2',index)">发布</button>
							<button class="Public-btn" v-if="item.status==2" @click="publishData(item.id,'1',index)">撤回</button>
							<button class="Public-btn" v-if="item.status==1" @click="delArticle(item.id)">删除</button>
						</td>
					</tr>
					<tr>
						<td colspan="8" style="color: red" v-if="!tableList.length">暂无数据</td>
					</tr>
				</tbody>
			</table>
  	</div>
  <Pageination  :total="model.total" :size="model.size" :page="model.page" :changge="pageFn"></Pageination>
  </div>
</template>
<script>
	import Pageination from "../shareComponent/pageination";
	export default {
    name:'insight',
    data:function(){
	    return{
	    	/**分页初始化**/
		    model: {
	        total: 0,//总数
	        size: 10,//每页显示条目个数不传默认20
	        page: 1,//当前页码
		    },
	      tableList:[],
	      /*检索内容*/
	    	searchContent:"",
	    	/*当前页码*/
	    	currentPage:1,
      }
    },
    created: function(){
    	$(window).scrollTop(0);
    },
    components:{Pageination},
    methods:{
    	/*搜索*/
    	searchList:function(){
    		this.model.page=1;
    		this.currentPage=1;
    		this.queryQuestionByType();
    	},
    	queryQuestionByType:function(){
    		this.$post(
          '/article/queryArticle',
	        {
	          "type":"14",
	          "page":this.model.page,
	          "size":this.model.size,
	          "orderBy":"create_at",
	          "searchContent":this.searchContent,
	         }
        )
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
          		if(!response.resultData.articleList){
          			this.tableList =[]
          		}else{
		            this.tableList = response.resultData.articleList;
		            for(var i=0;i<this.tableList.length;i++){
		            	this.tableList[i].publishAt=this.tableList[i].publishAt?this.formatTime(this.tableList[i].publishAt):"";
		            	this.tableList[i].fileDate=this.tableList[i].fileDate?this.formatTime(this.tableList[i].fileDate):"";
		            }
		          }
	            this.model.total = response.resultData.totalCount;
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        })
    	},
    	/*页码切换执行方法*/
			pageFn:function(val){
				this.model.page = parseInt(val);
				this.currentPage= parseInt(val);
				//此处调列表数据接口
				this.queryQuestionByType();
			},
			/*跳转页面*/
			insightFn:function(type,id){
				this.$router.push({path:"/main/createInsight??currentType="+type+"&id="+id});
			},
			/*删除*/
			delArticle:function(id){
				var _this=this;
				layer.confirm('您确定要删除这条数据吗？', {
					title:"删除",
					btnAlign:"c",
				  btn: ['确定','取消'] //按钮
				}, function(){
					_this.$get('/article/delArticle/'+id)
	    		.then(response => {
	    			/*未登录重定向*/
	          if(response==444){
	          	_this.$router.push({path: '/login'});
	          }else{
	          	if (response.resultCode ==0) {
	          		layer.alert("删除成功");
	          		
	          		var num = Math.ceil(_this.model.total / _this.model.size)-1;
	          		if(_this.model.total-(_this.model.size*num)>1||_this.tableList.length>1){
	          			_this.model.page=_this.currentPage;
	          		}else if(_this.model.total==1){
	          			_this.model.page==1
	          		}else{
	          			_this.model.page=parseInt(_this.currentPage)-1;
	          		}
			    			_this.queryQuestionByType();
		          }else{
		          	layer.alert(response.resultMsg);
		          }
	          }
	         
	        }).catch(err => {
	          console.log(err);
	        });
				});
			},
			/*撤回/发布*/
			publishData:function(id,status,index){
				var _this=this;
				if(status==1){
					this.title="撤回";
					this.content="您确定要撤回这条数据吗？";
				}else if(status==2){
					this.title="发布";
					this.content="您确定要发布这条数据吗？";
				}
				var indexs=layer.confirm(_this.content, {
					title:_this.title,
					btnAlign:"c",
				  btn: ['确定','取消'] //按钮
				}, function(){
					layer.close(indexs);
					_this.$get('/article/updatestatus/'+id+"/"+status)
	    		.then(response => {
	    			/*未登录重定向*/
	          if(response==444){
	          	_this.$router.push({path: '/login'});
	          }else{
	          	if (response.resultCode ==0) {
	          		if(status==1){
									layer.alert("撤回成功");
								}else if(status==2){
									layer.alert("发布成功");
								}
	          		_this.queryQuestionByType();
		          }else{
		          	layer.alert(response.resultMsg);
		          }
	          }
	         
	        }).catch(err => {
	          console.log(err);
	        });
				});
			},
		}
  }
</script>

<style  lang="less" scoped>
	
</style>

